//
//  LoginViewController.m
//  shuodi
//
//  Created by 孔晓寅 on 15/12/5.
//  Copyright © 2015年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import "LoginViewController.h"
#import "ForgetPasswdViewController.h"
#import "UINavigationBar+Awesome.h"
#import "WebViewController.h"
#import "CommoneTools.h"

@interface LoginViewController (){
    NSTimer *showTimer;
    int count ;
    
    NSTimer *showTimer_R;
    int count_R ;
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar lt_setBackgroundColor:[utils getThemeColor]];
    [self.navigationController.navigationBar setShadowImage:nil];
}

-(void)loginclick:(id)sender{
    [self.view endEditing:YES];
    
    if (_swithbutton.isOn) {//验证码登录
        [self codeloginRequest];
    }else{
        [self loginRequest];
    }

//    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
//    [appDelegate event_isloginok];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void) initControl {
    [self.view setBackgroundColor:[utils getBackGroupColor]];
    
    CGFloat bottom = kScreenW*9/16;
    
    UIImageView *backgroupView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, bottom)];
    [backgroupView setImage:[UIImage imageNamed:@"bg_2x"]];
    UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenW/4, 0, kScreenW/2, bottom)];
    logoView.contentMode =  UIViewContentModeScaleAspectFit;
    [logoView setImage:[UIImage imageNamed:@"logo_2x"]];
    [backgroupView addSubview:logoView];
    
    [self.view addSubview:backgroupView];
    
    _subtitle_segmentedControl = [[HYSegmentedControl alloc] initWithFrame:CGRectMake(0, bottom-44, kScreenW, 44) Bgcolor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3] Linecolor:[UIColor clearColor] Arrow:NO Titles:@[@"登录", @"注册" ] delegate:self];
    [self.view addSubview:_subtitle_segmentedControl];
//    [subtitle_segmentedControl changeSegmentedControlWithIndex:subsegmentedControl_index];
    

    _register_view = [[UIView alloc] initWithFrame:CGRectMake(0, bottom, kScreenW, kScreenH - bottom)];
    [self.view addSubview:_register_view];
    [_register_view setHidden:YES];
    
    _login_view = [[UIView alloc] initWithFrame:CGRectMake(0, bottom, kScreenW, kScreenH - bottom)];
    [self.view addSubview:_login_view];
    
    [self initLoginView];
    [self initRegisterView];
}

-(void)initLoginView{
    
    _swithbutton = [[UISwitch alloc] init];
    [_swithbutton sizeToFit];
    [_swithbutton setFrame:CGRectMake(kScreenW - _swithbutton.frame.size.width - OFFSET_TWN, (44-_swithbutton.frame.size.height)/2, _swithbutton.frame.size.width, _swithbutton.frame.size.height)];
    [_swithbutton addTarget:self action:@selector(switchclick:) forControlEvents:UIControlEventValueChanged];
    [_swithbutton setOn:NO];
    [_login_view addSubview:_swithbutton];
    
    UILabel *label = [utils getLabel:14 color:[utils getTextColorBlack]];
    label.frame = CGRectMake(OFFSET, 0, _swithbutton.frame.origin.x-2*OFFSET, 44);
    label.text = @"使用手机动态验证码登录";
    [_login_view addSubview:label];
    
    CGFloat bottom = 44;
    UIView *lbl = [[UIView alloc] initWithFrame:CGRectMake(0, bottom-1, kScreenW, 0.5)];
    lbl.backgroundColor =  [utils getLineColorGray];
    [_login_view addSubview:lbl];
    
    bottom += OFFSET_TWN;
    _usernameEdit = [[UITextField alloc] init];
    UIView *userName = [self setEdit:@"icon_shouji_2x" withview:_usernameEdit withtext:@"" withplaceholder:@"请输入手机号" withY:bottom];
    _usernameEdit.keyboardType = UIKeyboardTypeNumberPad;
    _usernameEdit.text = @"";
    [_login_view addSubview:userName];
    
    _clear_button = [utils getButton:CGRectMake(userName.frame.size.width - _usernameEdit.frame.size.height/2 , 0, _usernameEdit.frame.size.height/2, _usernameEdit.frame.size.height) withName:@""];
    _clear_button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _clear_button.backgroundColor = [UIColor clearColor];
    [_clear_button setBackgroundImage:nil forState:UIControlStateHighlighted];
    [_clear_button setBackgroundImage:nil forState:UIControlStateSelected];
    [_clear_button setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateNormal];
    
    [userName addSubview:_clear_button];
    [_clear_button addTarget:self action:@selector(clearclick:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:userName] + OFFSET_TWN;
    _passwdEdit = [[UITextField alloc] init];
    _passWord = [self setEdit:@"icon_mima_2x" withview:_passwdEdit withtext:@"" withplaceholder:@"请输入密码" withY:bottom];

    _passwdEdit.text = @"";
    _passwdEdit.secureTextEntry = YES;
    [_login_view addSubview:_passWord];
    
    _passwdhide_button = [utils getButton:CGRectMake(_passWord.frame.size.width - _passwdEdit.frame.size.height/2 , 0, _passwdEdit.frame.size.height/2, _passwdEdit.frame.size.height) withName:@""];
    _passwdhide_button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _passwdhide_button.backgroundColor = [UIColor clearColor];
    [_passwdhide_button setBackgroundImage:nil forState:UIControlStateHighlighted];
    [_passwdhide_button setBackgroundImage:nil forState:UIControlStateSelected];
    [_passwdhide_button setImage:[UIImage imageNamed:@"icon_browse_2x"] forState:UIControlStateNormal];
    [_passwdhide_button setImage:[UIImage imageNamed:@"icon_browse_xuanzhong_2x"] forState:UIControlStateSelected];
    
    [_passWord addSubview:_passwdhide_button];
    [_passwdhide_button addTarget:self action:@selector(psswitchclick:) forControlEvents: UIControlEventTouchUpInside];
    
    _codeEdit = [[UITextField alloc] init];
    _codeView = [self setEdit:@"icon_yanzhengma_2x" withview:_codeEdit withtext:@"" withplaceholder:@"请输入验证码" withY:bottom];
    _codeEdit.text = @"";
    _codeEdit.keyboardType = UIKeyboardTypeNumberPad;
    [_login_view addSubview:_codeView];
    
    _getcode_button = [utils getButton:CGRectMake(_codeView.frame.size.width - _codeEdit.frame.size.height*2 , 0, _codeEdit.frame.size.height*2, _codeEdit.frame.size.height) withName:@"获取验证码"];
    _getcode_button.titleLabel.font = [UIFont systemFontOfSize:13];
    _getcode_button.backgroundColor = [UIColor clearColor];
    [_getcode_button setTitleColor:[utils getButtonColor] forState:UIControlStateNormal];
    [_getcode_button setTitleColor:[utils getTextColorGray] forState:UIControlStateDisabled];
    
    [_codeView addSubview:_getcode_button];
    [_getcode_button addTarget:self action:@selector(getverification:) forControlEvents: UIControlEventTouchUpInside];
    [_codeView setHidden:YES];
    
    bottom = [utils getViewBottom:_passWord] + 20;
    
    _login_button = [utils getButton:CGRectMake(OFFSET_TWN, bottom, self.view.frame.size.width-OFFSET_TWN*2, _passwdEdit.frame.size.height) withName:@"登录"];
    [_login_view addSubview:_login_button];
    [_login_button addTarget:self action:@selector(loginclick:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:_login_button] + 10;
    _forget_button = [utils getButton:CGRectMake(OFFSET_TWN, bottom, self.view.frame.size.width/2, 44) withBgColor:[UIColor clearColor] withName:@"忘记密码？" ];
    [_forget_button setTitleColor:[utils getTextColorGray] forState:UIControlStateNormal];
    _forget_button.titleLabel.font = [UIFont systemFontOfSize:14];
    [_forget_button sizeToFit];
    [_forget_button setFrame:CGRectMake((kScreenW - _forget_button.frame.size.width)/2, bottom, _forget_button.frame.size.width, _forget_button.frame.size.height)];
    [_login_view addSubview:_forget_button];
    [_forget_button addTarget:self action:@selector(forgetclick:) forControlEvents: UIControlEventTouchUpInside];
}

-(void)initRegisterView{
    
    CGFloat bottom = OFFSET_TWN;
    
    _usernameEdit_R = [[UITextField alloc] init];
    UIView *userName = [self setEdit:@"icon_shouji_2x" withview:_usernameEdit_R withtext:@"" withplaceholder:@"请输入手机号" withY:bottom];
    _usernameEdit_R.text = @"";
    _usernameEdit_R.keyboardType = UIKeyboardTypeNumberPad;
    [_register_view addSubview:userName];
    
    _clear_button_R = [utils getButton:CGRectMake(userName.frame.size.width - _usernameEdit_R.frame.size.height/2 , 0, _usernameEdit_R.frame.size.height/2, _usernameEdit_R.frame.size.height) withName:@""];
    _clear_button_R.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _clear_button_R.backgroundColor = [UIColor clearColor];
    [_clear_button_R setBackgroundImage:nil forState:UIControlStateHighlighted];
    [_clear_button_R setBackgroundImage:nil forState:UIControlStateSelected];
    [_clear_button_R setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateNormal];
    //    [_clear_button setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateSelected];
    
    [userName addSubview:_clear_button_R];
    [_clear_button_R addTarget:self action:@selector(clearclick_R:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:userName] + OFFSET_TWN;
    
    _codeEdit_R = [[UITextField alloc] init];
    UIView *codeview = [self setEdit:@"icon_mima_2x" withview:_codeEdit_R withtext:@"" withplaceholder:@"请输入验证码" withY:bottom];
    _codeEdit_R.text = @"";
    _codeEdit_R.keyboardType = UIKeyboardTypeNumberPad;
    [_register_view addSubview:codeview];
    
    _getcode_button_R = [utils getButton:CGRectMake(codeview.frame.size.width - _codeEdit_R.frame.size.height*2, 1, _codeEdit_R.frame.size.height*2, _codeEdit_R.frame.size.height-2) withName:@"获取验证码"];
    _getcode_button_R.titleLabel.textAlignment = NSTextAlignmentRight;
    _getcode_button_R.titleLabel.font = [UIFont systemFontOfSize:13];
    _getcode_button_R.backgroundColor = [UIColor clearColor];
    [_getcode_button_R setTitleColor:[utils getButtonColor] forState:UIControlStateNormal];
    [_getcode_button_R setTitleColor:[utils getTextColorGray] forState:UIControlStateDisabled];
    [codeview addSubview:_getcode_button_R];
    [_getcode_button_R addTarget:self action:@selector(getverification_R:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:codeview] + OFFSET_TWN;
    
    _passwdEdit_R = [[UITextField alloc] init];
    UIView *passWord = [self setEdit:@"icon_yanzhengma_2x" withview:_passwdEdit_R withtext:@"" withplaceholder:@"请输入密码" withY:bottom];
    _passwdEdit_R.secureTextEntry = YES;
    _passwdEdit_R.text = @"";
    [_register_view addSubview:passWord];
    
    _passwdhide_button_R = [utils getButton:CGRectMake(passWord.frame.size.width - _passwdEdit_R.frame.size.height/2, 0, _passwdEdit_R.frame.size.height/2, _passwdEdit_R.frame.size.height) withName:@""];
    _passwdhide_button_R.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _passwdhide_button_R.backgroundColor = [UIColor clearColor];
    [_passwdhide_button_R setBackgroundImage:nil forState:UIControlStateHighlighted];
    [_passwdhide_button_R setBackgroundImage:nil forState:UIControlStateSelected];
    [_passwdhide_button_R setImage:[UIImage imageNamed:@"icon_browse_2x"] forState:UIControlStateNormal];
    [_passwdhide_button_R setImage:[UIImage imageNamed:@"icon_browse_xuanzhong_2x"] forState:UIControlStateSelected];
    [passWord addSubview:_passwdhide_button_R];
    [_passwdhide_button_R addTarget:self action:@selector(switchclick_R:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:passWord] + OFFSET_TWN;
    
    _register_button_R = [utils getButton:CGRectMake(OFFSET_TWN, bottom, self.view.frame.size.width-OFFSET_TWN*2, _passwdEdit_R.frame.size.height) withName:@"注册"];
    [_register_view addSubview:_register_button_R];
    [_register_button_R addTarget:self action:@selector(registerclick:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:_register_button_R] + 20;
    _agree_text_R = [utils getLabel:14 color:[utils getTextColorBlack]];
    _agree_text_R.text = @"轻触上面的“注册”按钮，即表示你同意";
    [_agree_text_R sizeToFit];
    [_agree_text_R setFrame:CGRectMake((self.view.frame.size.width - _agree_text_R.frame.size.width)/2, bottom, _agree_text_R.frame.size.width, _agree_text_R.frame.size.height)];
    [_register_view addSubview:_agree_text_R];
    
    bottom = [utils getViewBottom:_agree_text_R];
    _agree_button_R = [UIButton buttonWithType:UIButtonTypeCustom];
//    [_agree_button_R setImage:[UIImage imageNamed:@"xiaoyuan"] forState:UIControlStateNormal];
//    [_agree_button_R setImage:[UIImage imageNamed:@"xuanzhong_2x"] forState:UIControlStateSelected];
    [_agree_button_R setTitle:@"《指尖苏州许可及服务协议》" forState:UIControlStateNormal];
    [_agree_button_R setTitleColor:[UIColor colorWithRed:96.0/255.0 green:115.0/255.0 blue:152.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    _agree_button_R.titleLabel.font = [UIFont systemFontOfSize:14];
    [_agree_button_R sizeToFit];
    CGFloat button_w = _agree_button_R.frame.size.width;
    CGFloat button_h = _agree_button_R.frame.size.height;
    [_agree_button_R setFrame:CGRectMake((kScreenW-button_w)/2, bottom, button_w, button_h)];
//    [_agree_button_R setSelected:YES];
    [_register_view addSubview:_agree_button_R];
    [_agree_button_R addTarget:self action:@selector(agreeclick:) forControlEvents: UIControlEventTouchUpInside];
    
}

-(UIView *)setEdit:(NSString *)img_url withview:(UITextField*)subview withtext:(NSString*)text withplaceholder:(NSString*)placeholder withY:(CGFloat)point_y{
    
    CGFloat left_x = 50;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(left_x, point_y, self.view.frame.size.width-left_x*2, 44)];
    [view setBackgroundColor:[UIColor whiteColor]];
    
    UIImage *img = [UIImage imageNamed:img_url];
    CGFloat icon_img_h = view.frame.size.height - 24;
    CGFloat icon_img_w = img.size.width/img.size.height*icon_img_h;
    UIImageView *icon_img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 12, icon_img_w, icon_img_h)];
    [icon_img setImage:img];
    
    [subview setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    subview.text = text;
    subview.textColor = [UIColor blackColor];
    subview.font = [UIFont systemFontOfSize:15];
    subview.placeholder = placeholder;
    [subview.layer setCornerRadius:5];
    [subview.layer setBorderWidth:0];
    [subview.layer setBorderColor:[[utils getBorderColorGray] CGColor]];
    [subview setBackgroundColor:[UIColor whiteColor]];
    subview.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 24, 0)];
    subview.leftViewMode = UITextFieldViewModeAlways;
    subview.delegate = self;
    //    [subview addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    subview.returnKeyType = UIReturnKeyDone;
    
    [view addSubview:subview];
    [view addSubview:icon_img];
    
    UIView *lbl = [[UIView alloc] initWithFrame:CGRectMake(0, 43, view.frame.size.width, 0.5)];
    lbl.backgroundColor =  [utils getLineColorGray];
    [view addSubview:lbl];

    
    return view;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
//    CGRect frame = [textField superview].frame;
    CGRect frame = [self.view convertRect:textField.frame fromView:[textField superview]];
    //    CGFloat offset_y = self.myScrollView.contentOffset.y;
    
    //在这里我多加了62，（加上了输入中文选择文字的view高度）这个依据自己需求而定
    int offset = (frame.origin.y+120)-(self.view.frame.size.height-216.0);//键盘高度216
    
    if (offset>0) {
        
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        
        [UIView setAnimationDuration:0.30f];//动画持续时间
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    //输入框编辑完成以后，当键盘即将消失时，将视图恢复到原始状态
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
}

-(void)codeloginRequest{
    
    if (![CommoneTools isConnectionAvailable]) {
        [OMGToast showWithText:@"登录失败，请检查网络" bottomOffset:50 duration:1];
        return;
    }
    
    if ([_usernameEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if ([_codeEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入验证码" bottomOffset:50 duration:1];
        return;
    }
    
    [HttpNetWorkUtil requestcodeLogin:@{@"phone":_usernameEdit.text,@"code":_codeEdit.text} success:^(id json) {
        
        InfoDataModel *infodatamodel = [[InfoDataModel alloc] init];
        [infodatamodel fillWithJsonDict:json];
        
        [utils setString:infodatamodel.infomodel.token forkey:TOKEN];
        
        NSData *data = [RFJModel toDataWithModel:infodatamodel];
        [utils setObject:data forkey:PERSONINFO];

        //重新绑定channelID
        NSString *channelId = [utils getString:CHANNELID];
        
        if (channelId) {
            AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
            [appDelegate BindChannelID:channelId];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } fail:^{}];
}

-(void)loginRequest{
    
    if ([_usernameEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if (![utils checkTelNumber:_usernameEdit.text]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }

    if ([_passwdEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入6位以上数字或字母密码" bottomOffset:50 duration:1];
        return;
    }
    

    [HttpNetWorkUtil requestlogin:@{@"phone":_usernameEdit.text,@"pwd":_passwdEdit.text} success:^(id json) {

        
        BaseModel *model = [[BaseModel alloc] init];
        [model fillWithJsonDict:json];
        
        if([model.code isEqualToString:@"10003"]){//账号还未注册
            
      
        
            DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"" contentText:@"该账号未注册，是否立即注册？" leftButtonTitle:@"取消" rightButtonTitle:@"确认"];
            [alert changeStyle];
            [alert show];
            alert.leftBlock = ^() {
            };
            alert.rightBlock = ^() {
                
                [_subtitle_segmentedControl changeSegmentedControlWithIndex:1];
            };
            alert.dismissBlock = ^() {
     
            };
            
        }else if([model.code isEqualToString:@"10002"]){//密码错误
            NSString *message =@"请输入正确的登录密码";
            
            [OMGToast showWithText:message bottomOffset:50 duration:1];
        

        }else{
            InfoDataModel *infodatamodel = [[InfoDataModel alloc] init];
            [infodatamodel fillWithJsonDict:json];
            
            [utils setString:infodatamodel.infomodel.token forkey:TOKEN];
            
            NSData *data = [RFJModel toDataWithModel:infodatamodel];
            [utils setObject:data forkey:PERSONINFO];
            
            //重新绑定channelID
            NSString *channelId = [utils getString:CHANNELID];
            
            if (channelId) {
                AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
                [appDelegate BindChannelID:channelId];
            }
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }

    } fail:^{}];
    
}

-(void) setchannel{
}

-(IBAction)forgetclick:(id)sender{
    ForgetPasswdViewController *view = [[ForgetPasswdViewController alloc] init];
    [self.navigationController pushViewController:view animated:YES];
}

-(IBAction)switchclick:(id)sender{
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    if (isButtonOn) {
        [_codeView setHidden:NO];
        [_passWord setHidden:YES];
    }else {
        [_codeView setHidden:YES];
        [_passWord setHidden:NO];
    }
}

-(IBAction)psswitchclick:(id)sender{
    
    UIButton *Button = (UIButton*)sender;
    if ([Button isSelected]) {
        [Button setSelected:NO];
        _passwdEdit.secureTextEntry = YES;
    }else{
        [Button setSelected:YES];
        _passwdEdit.secureTextEntry = NO;
    }
    
}

-(IBAction)clearclick:(id)sender{
    
    _usernameEdit.text = @"";
}

-(IBAction)clearclick_R:(id)sender{
    
    _usernameEdit_R.text = @"";
}

-(void)getverification:(id)sender {
    
    [_usernameEdit resignFirstResponder];
    [_passwdEdit resignFirstResponder];
    
    if ([_usernameEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if (![utils checkTelNumber:_usernameEdit.text]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    [HttpNetWorkUtil requestgetVerificationCode:@{@"phone":_usernameEdit.text,@"codeType":@"4"} success:^(id json) {
        [self initTimer];
    } fail:^{}];
    
}

-(void)initTimer
{
    count = 60;
    //时间间隔
    NSTimeInterval timeInterval =1.0 ;
    //定时器
    _getcode_button.enabled = NO;
    [_getcode_button setTitle:[NSString stringWithFormat:@"重新发送(%d)",count] forState:UIControlStateNormal];
    showTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                                 target:self
                                               selector:@selector(handleTimer:)
                                               userInfo:nil
                                                repeats:YES];
}

-(void)handleTimer:(id)sender {
    count --;
    if (count <= 0 ) {
        [showTimer invalidate];
        _getcode_button.enabled = YES;
        [_getcode_button setTitle:@"重新获取" forState:UIControlStateNormal];
    } else {
        [_getcode_button setTitle:[NSString stringWithFormat:@"重新发送(%d)",count] forState:UIControlStateNormal];
    }
}

- (void)hySegmentedControlSelectAtIndex:(NSInteger)index withid:(id)hysegcon
{
    [self.searchBar resignFirstResponder];
    
    HYSegmentedControl *temp =(HYSegmentedControl *)hysegcon;
    if(temp == _subtitle_segmentedControl){
        
        if (index == 1) {
            [_login_view setHidden:YES];
            [_register_view setHidden:NO];
            
            _usernameEdit_R.text = _usernameEdit.text;
        }else{
            [_login_view setHidden:NO];
            [_register_view setHidden:YES];
            
            _usernameEdit.text = _usernameEdit_R.text;
        }
    }
}

-(IBAction)agreeclick:(id)sender{
    
    if ([(AppDelegate *)[UIApplication sharedApplication].delegate isConnectionAvailable]) {
        WebViewController *view = [[WebViewController alloc] initWithUrl:@"http://58.211.5.134:8080/bestpayTicket_front/login/goRegisterRule" withTitle:nil];
        [self.navigationController pushViewController:view animated:YES];
    }
}

-(IBAction)switchclick_R:(id)sender{
    UIButton *Button = (UIButton*)sender;
    if ([Button isSelected]) {
        [Button setSelected:NO];
        _passwdEdit_R.secureTextEntry = YES;
    }else{
        [Button setSelected:YES];
        _passwdEdit_R.secureTextEntry = NO;
    }
}

-(IBAction)registerclick:(id)sender{
    
    [self.usernameEdit_R resignFirstResponder];
    [self.passwdEdit_R resignFirstResponder];
    
    if ([_usernameEdit_R.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if (![utils checkTelNumber:_usernameEdit_R.text]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if ([_passwdEdit_R.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入6位以上数字或字母密码" bottomOffset:50 duration:1];
        return;
    }
    
    if ([_codeEdit_R.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入验证码" bottomOffset:50 duration:1];
        return;
    }
 

    [HttpNetWorkUtil requestregisterMember:@{@"phone":_usernameEdit_R.text,@"pwd":_passwdEdit_R.text,@"code":_codeEdit_R.text} success:^(id json) {

        
        InfoDataModel *infodatamodel = [[InfoDataModel alloc] init];
        [infodatamodel fillWithJsonDict:json];
        
        if([infodatamodel.code isEqualToString:@"40002"]){//手机号重复
            
            if (_usernameEdit_R.isFirstResponder || _passwdEdit_R.isFirstResponder) {
                [OMGToast showWithText:@"请该账号已存在，请直接登录" bottomOffset:300 duration:1];
            }else{
                  [OMGToast showWithText:@"请该账号已存在，请直接登录" bottomOffset:50 duration:1];
            }
            
            
            
            
            
        }else if([infodatamodel.code isEqualToString:@"40003"]){//验证码错误
            NSString *message =@"验证码不对哦\n重新输入吧！";
            DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"" contentText:message leftButtonTitle:nil rightButtonTitle:@"好哒"];
            [alert show];
            alert.leftBlock = ^() {
            };
            alert.rightBlock = ^() {
            };
            alert.dismissBlock = ^() {
                NSLog(@"Do something interesting after dismiss block");
            };
        }else{
            [utils setString:infodatamodel.infomodel.token forkey:TOKEN];
            
            NSData *data = [RFJModel toDataWithModel:infodatamodel];
            [utils setObject:data forkey:PERSONINFO];
            
            //重新绑定channelID
            NSString *channelId = [utils getString:CHANNELID];
            
            if (channelId) {
                AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
                [appDelegate BindChannelID:channelId];
            }
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
    } fail:^{}];
//    NSDictionary *dict = @{@"account":_usernameEdit_R.text,@"password":_passwdEdit_R.text,@"code":_codeEdit_R.text};
//    [HttpNetWorkUtil requestreg:dict success:^(id json) {
//        
//        NSString *token = [json objectForKey:@"data"];
//        [utils setString:token forkey:TOKEN];
//        [self.navigationController popToRootViewControllerAnimated:YES];
//    } fail:^{}];
    
}

-(void)getverification_R:(id)sender {
    
    [self.usernameEdit_R resignFirstResponder];
    [self.passwdEdit_R resignFirstResponder];
    [self.codeEdit_R resignFirstResponder];
    
    if ([_usernameEdit_R.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if (![utils checkTelNumber:_usernameEdit_R.text]) {
        [OMGToast showWithText:@"请输入正确的手机号" bottomOffset:50 duration:1];
        return;
    }
    

    [HttpNetWorkUtil requestgetVerificationCode:@{@"phone":_usernameEdit_R.text,@"codeType":@"1"} success:^(id json) {
        [self initTimer_R];
    } fail:^{}];
   
}

-(void)initTimer_R
{
    count_R = 60;
    //时间间隔
    NSTimeInterval timeInterval =1.0 ;
    //定时器
    _getcode_button_R.enabled = NO;
    [_getcode_button_R setTitle:[NSString stringWithFormat:@"重新发送(%d)",count_R] forState:UIControlStateNormal];
    showTimer_R = [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                                 target:self
                                               selector:@selector(handleTimer_R:)
                                               userInfo:nil
                                                repeats:YES];
}

-(void)handleTimer_R:(id)sender {
    count_R --;
    if (count_R <= 0 ) {
        [showTimer_R invalidate];
        _getcode_button_R.enabled = YES;
        [_getcode_button_R setTitle:@"重新获取" forState:UIControlStateNormal];
    } else {
        [_getcode_button_R setTitle:[NSString stringWithFormat:@"重新发送(%d)",count_R] forState:UIControlStateNormal];
    }
}

@end
